/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.tools;

import java.awt.MenuItem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class RdpFileWriterTool extends Tool {

	JMenuItem mnu_tunnels = new JMenuItem("Write RDP file");
	MenuItem mnu_tray = new MenuItem("Write RDP file");

	public RdpFileWriterTool() {
		super("RdpFileWriterTool", "RdpFileWriterTool");
	}

	public void write_to(File file) throws IOException {
		FileOutputStream stream = new FileOutputStream(file);
		PrintStream out = new PrintStream(stream);

		this.write_to(out);

		out.close();
		stream.close();
	}

	private void write_to(PrintStream out) throws IOException {
		out.println("screen mode id:i:2");
		out.println("desktopwidth:i:1280");
		out.println("desktopheight:i:1024");
		out.println("session bpp:i:16");
		out.println("winposstr:s:0,1,162,23,1204,926");
		out.println("full address:s:127.0.0.1:"
				+ this.currentTunnel.getLocalPort());
		out.println("compression:i:1");
		out.println("keyboardhook:i:2");
		out.println("audiomode:i:0");
		out.println("redirectprinters:i:1");
		out.println("redirectcomports:i:0");
		out.println("redirectsmartcards:i:1");
		out.println("redirectclipboard:i:1");
		out.println("redirectposdevices:i:0");
		out.println("displayconnectionbar:i:1");
		out.println("autoreconnection enabled:i:1");
		out.println("authentication level:i:0");
		out.println("prompt for credentials:i:0");
		out.println("negotiate security layer:i:1");
		out.println("remoteapplicationmode:i:0");
		out.println("alternate shell:s:");
		out.println("shell working directory:s:");
		out.println("disable wallpaper:i:1");
		out.println("disable full window drag:i:1");
		out.println("allow desktop composition:i:0");
		out.println("allow font smoothing:i:0");
		out.println("disable menu anims:i:1");
		out.println("disable themes:i:0");
		out.println("disable cursor setting:i:0");
		out.println("bitmapcachepersistenable:i:1");
		out.println("gatewayhostname:s:");
		out.println("gatewayusagemethod:i:0");
		out.println("gatewaycredentialssource:i:4");
		out.println("gatewayprofileusagemethod:i:0");
	}

	@Override
	public JMenuItem get_tunnel_tools_menu(boolean server_connected,
			boolean tunnel_connected) {
		return this.mnu_tunnels;
	}

	@Override
	public JMenuItem get_server_tools_menu(boolean server_connected) {
		return null;
	}

	@Override
	public MenuItem get_tray_tools_menu() {
		return this.mnu_tray;
	}

	@Override
	public JMenuItem get_main_tools_menu() {
		return null;
	}

	@Override
	public void run() {
		if (this.currentTunnel == null) { /* Called from tray */
			this.currentTunnel = this
					.ask_tunnel("Which tunnel do you want to use to create this file?");
		}

		File selection = this.ask_file(".rdp", "RDP Files");
		try {
			this.write_to(selection);
			JOptionPane.showMessageDialog(null, "File written.");
		} catch (IOException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage(),
					"Writing aborted ...", JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void init_properties() {
	}
}
